//
//  HomeCtrl.m
//  LaoWu
//
//  Created by yinqy on 15/4/23.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "HomeCtrl.h"
#import "ProjectDetailCtrl.h"
#import "TrainInfoCtrl.h"
#import "AboutUsCtrl.h"
#import "ResumeCenterCtrl.h"
#import "RecruitmentCtrl.h"
#import "LWNewProjectCtrl.h"
#import "LWWeatherCtrl.h"
#import "LWLoginCtrl.h"
#import "ResumeListCtrl.h"
#import "ProjectListCtrl.h"
#import <BaiduMapAPI/BMapKit.h>//refer all head file

@interface HomeCtrl ()<BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>
@property(nonatomic,strong)BMKLocationService *locService;
@end

@implementation HomeCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"工友通"];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self initBaiduMapLocation];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImageName:@"lw_tianqi" highImageName:@"lw_tianqi" target:self action:@selector(weather)];
   
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    NSInteger group_id = [UserDefaultsHelper integerForKey:KEY_USER_GROUP_ID];
//    if(group_id == 3||group_id == 10||group_id==11||group_id==12||group_id==13){
//        [_identifyCheckLabel setText:@"工程信息发布"];
//    }else if(group_id == 1){
//        [_identifyCheckLabel setText:@"简历中心"];
//    }else{
//        [_identifyCheckLabel setText:@"简历中心"];
//    }
    
    NSString *city = [UserDefaultsHelper stringForKey:kLocationAddressCity];
    if(!ISBLANK(city)){
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:city style:UIBarButtonItemStyleDone target:nil action:nil];
        self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    }else{
        self.navigationItem.leftBarButtonItem = nil;
    }
    
}


-(void)initBaiduMapLocation{
    //set location accuracy，default：kCLLocationAccuracyBest
    [BMKLocationService setLocationDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    //assign min distance update(meter)，default：kCLDistanceFilterNone
    [BMKLocationService setLocationDistanceFilter:100.f];
    //init BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    //start LocationService
    [_locService startUserLocationService];
}

//implemention relevance delegate, deal with location info update ,deal with orientation update info
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    //NSLog(@"heading is %@",userLocation.heading);
}
//deal with location info update
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    //NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    if(userLocation.location){
        NSLog(@"\n百度到位置:\n  纬度lat:%f\n  经度lng:%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
        BMKGeoCodeSearch *geoCodeSearch = [[BMKGeoCodeSearch alloc]init];
        geoCodeSearch.delegate  = self;
        //initialize geography encode class
        BMKReverseGeoCodeOption *reverseGeoCodeOption = [[BMKReverseGeoCodeOption alloc]init];
        //need backwards geography encode's location
        reverseGeoCodeOption.reverseGeoPoint = userLocation.location.coordinate;
        [geoCodeSearch reverseGeoCode:reverseGeoCodeOption];
        [_locService stopUserLocationService];
    }
    
}

-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    //get city
    NSString* city =  result.addressDetail.city;
    [UserDefaultsHelper setObject:city forKey:kLocationAddressCity];
}


-(void)weather{
    LWWeatherCtrl *ctrl = [[LWWeatherCtrl alloc] init];
    [self.navigationController pushViewController:ctrl animated:YES];
}

- (IBAction)zhaopin:(id)sender {
    ProjectListCtrl *ctrl = [[ProjectListCtrl alloc]init];
    [self.navigationController pushViewController:ctrl animated:YES];
    NSLog(@"招聘点击");
}

- (IBAction)qiuzhi:(id)sender {
    ResumeListCtrl *ctrl = [[ResumeListCtrl alloc] init];
    [self.navigationController pushViewController:ctrl animated:YES];
    
//    NSInteger group_id = [UserDefaultsHelper integerForKey:KEY_USER_GROUP_ID];
//    if(group_id == 3||group_id == 10||group_id==11||group_id==12||group_id==13){
//        NSLog(@"工程信息发布");
//        LWNewProjectCtrl *ctrl = [[LWNewProjectCtrl alloc] init];
//        [self.navigationController pushViewController:ctrl animated:YES];
//    }else{
//        if (!ISLOGIN) {
//            LWLoginCtrl *ctrl = [[LWLoginCtrl alloc] init];
//            [self presentViewController:[[CustomNavigationCtrl alloc] initWithRootViewController:ctrl] animated:YES completion:nil];
//            return;
//        }
//        ResumeCenterCtrl *ctrl =  [[ResumeCenterCtrl alloc]init];
//        [self.navigationController pushViewController:ctrl animated:YES];
//    }
}

- (IBAction)peixun:(id)sender {
    NSLog(@"培训点击");
    [self.navigationController pushViewController:[TrainInfoCtrl alloc] animated:YES];
}

- (IBAction)aboutus:(id)sender {
    //click about us
    [self.navigationController pushViewController:[AboutUsCtrl alloc] animated:YES];
    NSLog(@"关于我们");
}

@end
